using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PlainConcepts.Fx.Dnie.SmartCard.Interop
{
	/// <summary>
	/// Envoltorio de PC/SC utilizando winscard.dll
	/// </summary>
	class PersonalComputerSmartCard
	{
		[DllImport("winscard.dll")]
		public static extern Int32 SCardBeginTransaction(
			IntPtr card);

		[DllImport("winscard.dll")]
		public static extern Int32 SCardDisconnect(
			IntPtr card,
			UInt32 disposition);

		[DllImport("winscard.dll")]
		public static extern Int32 SCardEndTransaction(
			IntPtr card,
			UInt32 disposition);

		[DllImport("WinScard.dll")]
		public static extern int SCardListReaderGroups(IntPtr hContext, ref string cGroups, ref int nStringSize);

		[DllImport("WinScard.dll")]
		public static extern int SCardListReaders(IntPtr hContext, string cGroups,
			ref string cReaderLists, ref int nReaderCount);

		[DllImport("winscard.dll")]
		public static extern Int32 SCardEstablishContext(
			UInt32 scope,
			[MarshalAs(UnmanagedType.AsAny)] Object reserved1,
			[MarshalAs(UnmanagedType.AsAny)] Object reserved2,
			ref IntPtr pcscContext);

		[DllImport("winscard.dll")]
		public static extern Int32 SCardGetCardTypeProviderNameW(
			IntPtr pcscContext,
			string cardName,
			UInt32 providerId,
			[MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)] 
                ref string provider,
			ref UInt32 providerLength);

		[DllImport("winscard.dll")]
		public static extern Int32 SCardListCardsW(
			IntPtr pcscContext,
			byte[] atr,
			PCSCGuid[] interfaces,
			UInt32 interfaceCount,
			[MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 5)] 
                ref string cards,
			ref UInt32 cardsLength);

		[DllImport("winscard.dll")]
		public static extern Int32 SCardReleaseContext(
			IntPtr pcscContext);

		[DllImport("winscard.dll", CharSet = CharSet.Unicode)]
		public static extern Int32 SCardStatusW(
			IntPtr cardHandle,
			[MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 2)] [In, Out]
                string readerName,
			  ref UInt32 readerNameLength,
			  ref UInt32 state,
			  ref UInt32 protocol,
			  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 6)] [In, Out]
                byte[] atr,
				ref UInt32 atrLength);
	}
}
